package com.ye.zcswzlms.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description
 * @Author by 叶德辉
 * @Date 2020/4/14
 **/
@RestController
@RequestMapping(value = "/file",method = RequestMethod.POST)
public class UploadFileController {
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file){
        if (file==null){
            return "upload false";
        }
        try {
            String filename = file.getOriginalFilename();
            String extFileName = filename.substring(filename.lastIndexOf("." ) +1,filename.length());
            System.out.println("文件名:\t"+filename);
            System.out.println("后缀名:\t"+extFileName);

            //上传到本地,模拟上传到fastdfs文件服务器
            String projectPath = System.getProperty("user.dir");
            String filePath= projectPath+"\\src\\main\\resources\\static\\static\\img\\";
            System.out.println(filePath);
            String path = filePath + filename;
            //文件存储路径

            File dest = new File(path);
            if (!dest.getParentFile().exists()){
                dest.getParentFile().mkdir();
            }
            file.transferTo(dest);
            return "\\static\\img\\"+filename;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload false";
    }


}
